package com.doom.a3rdlibdemo.ui.main;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.nfc.cardemulation.HostNfcFService;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.doom.a3rdlibdemo.R;
import com.doom.a3rdlibdemo.databinding.MainFragmentBinding;

import org.w3c.dom.Text;

public class MainFragment extends Fragment {
    private static final String TAG = MainFragment.class.getSimpleName();

    private MainViewModel mViewModel;
    private MainFragmentBinding mMainFragmentBinding;
    private Handler mHandler;

    public MainFragment() {
        super();
        mHandler = new Handler();
    }

    public MainFragment(int contentLayoutId) {
        super(contentLayoutId);
    }

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    private TextView mMessageTV;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
//        return inflater.inflate(R.layout.main_fragment, container, false);
        mMainFragmentBinding = DataBindingUtil.inflate(inflater, R.layout.main_fragment, container, false);
        View view = mMainFragmentBinding.getRoot();
        mMessageTV = (TextView)view.findViewById(R.id.message);
        View btn = view.findViewById(R.id.msg_btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mViewModel.setMessage("yyy");
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        mViewModel = new ViewModelProvider(this).get(MainViewModel.class);

        mViewModel = new ViewModelProvider(this,
                new ViewModelProvider.AndroidViewModelFactory(getActivity().getApplication())).get(MainViewModel.class);
        mMainFragmentBinding.setVm(mViewModel);
        mMainFragmentBinding.setLifecycleOwner(this);

        // 数据粘性
//        mViewModel.setMessage("XXX");

        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mViewModel.getPhoneInfo().observe(getActivity(), new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        Log.d(TAG, "onChanged s:"+s);
//                        new Exception("onChangedonChanged").printStackTrace();
        //                mMessageTV.setText(s);
                    }
                });
            }
        }, 3000);
    }



}



















